<?php
/*
 * Created on
 * Date: 2007-12-4
 * Author: William Yang
 */
require_once(dirname(__FILE__)."/../common/constants.php");
require_once(DB_SESSION_DIR_PATH."/db_operator.php");
require_once(DB_SESSION_DIR_PATH."/db_obj_defs.php");
require_once(TRANSACTIONS_DIR_PATH."/trans_common.php");

class MatchDirectRequestManager extends MatchDirectRequest {
	public function register() {
		$stat = parent::register(CONFIGURATION_DIR_PATH."/create_match_direct_request.xml",
								FALSE);
		return $stat;
	}
	
	public function query() {
		$db_opera = new DatabaseOperator();
		$db_opera->config(CONFIGURATION_DIR_PATH."/match_direct_request_table.xml");
		$cList = new SqlCondList();
		if ($this->requesteeid != NULL)
			$cList->add_condition(new SqlCondition("requesteeid", SqlCondition::EQUAL, $this->requesteeid));
		if ($this->requesterid != NULL) {
			if ($this->requesteeid != NULL)
				$cList->add_relation(SqlCondition::_AND);
			$cList->add_condition(new SqlCondition("requesterid", SqlCondition::EQUAL, $this->requesterid));
		}
		if ($this->matchid != NULL) {
			if ($this->requesteeid != NULL || $this->requesterid != NULL)
				$cList->add_relation(SqlCondition::_AND);
			$cList->add_condition(new SqlCondition("matchid", SqlCondition::EQUAL, $this->matchid));
		}
		if ($this->status != NULL) {
			if ($this->requesteeid != NULL || $this->requesterid != NULL || $this->matchid != NULL)
				$cList->add_relation(SqlCondition::_AND);
			$cList->add_condition(new SqlCondition("status", SqlCondition::EQUAL, $this->status));
		}	
		
		$rows = $db_opera->select($cList);
		
		$ret_list = array();
		$delegate = new MatchDirectRequest();
		foreach ($rows as $row) {
			$ret_list[] = $delegate->array2copy($row);
		}
		
		return $ret_list;
	}
	
	public static function query_by_muids($uids) {
		$operator = new DatabaseOperator();
		$operator->config(CONFIGURATION_DIR_PATH."/match_direct_request_table.xml");
		
		$condList = new SqlCondList();
		if (gettype($uids) === gettype(array())) {
			$cnt = count($uids);
			for ($i = 0; $i < $cnt - 1; ++$i) {
				$condList->add_condition(new SqlCondition('matchid', SqlCondition::EQUAL, $uids[$i]));
				$condList->add_relation(SqlCondition::_OR);
			}
			$condList->add_condition(new SqlCondition('matchid', SqlCondition::EQUAL, $uids[$cnt - 1]));
		} else
			$condList->add_condition(new SqlCondition('matchid', SqlCondition::EQUAL, $uids));
		
		$rows = $operator->select($condList);
		
		$ret = array();
		if ($rows === NULL) {
			return $ret;
		} else {
			$delegate = new MatchDirectRequest();
			if ($rows != null && count($rows) > 0) {
				foreach ($rows as $row) {
					$tmp = $delegate->array2copy($row);
					$ret[] = $tmp;
				}
			}
			
			return $ret;
		}
	}
}
?>
